(function($) {
	//这里是用户头像URL，例如http://eu.sy/public/image/avatar/{{id}}.png，其中{{id}}代表用户ID
	var avatarUrl = '{{avatarUrl}}';
	var OAuthWindow = null;
	//基础跨域解决
	var rootDomainReg = /([a-z0-9\-_]+)\.(com|net|edu|org|gov)\.([a-z]{1,5})$/i;
	if (rootDomainReg.test(window.location.hostname)) {
		var rootDomain = window.location.hostname.match(rootDomainReg)[0];
		document.domain = rootDomain;
	} else {
		rootDomainReg = /([a-z0-9\-_]+)\.([a-z]{1,5})$/i;
		var rootDomain = window.location.hostname.match(rootDomainReg)[0];
		document.domain = rootDomain;
	}
	//判断是否已经登录，展现不同的页面
	if (window.eu.isLogin) {
		$('.logined').find('.avatar').attr('src', avatarUrl.replace('{{id}}', window.eu.uid));
		var timeout = 3;
		var t = setInterval(function() {
			timeout--;
			if (timeout === 0) {
				clearInterval(t);
				$('#useOther').attr('disabled', 'true');
				window.parent[window.eu.callback]();
			}
			$('.logined').find('span').html(timeout);
		}, 1000);
		$('#useOther').bind('click', function() {
			clearInterval(t);
			$('.logined').hide();
			$('.login-form').show();
		});
		$('.logined').show();
		$('.login-form').hide();
	}
	//登录按钮点击事件
	$('#submit').bind('click',
	function() {
		$(this).attr('disabled', 'true');
		$(this).html('请稍候 ...');
		var user = {
			"_csrf_token": $('meta[name="_csrf_token"]').attr('content'),
			"user": $('#user').val(),
			"password": $('#password').val(),
			"appid": window.eu.appid
		};
		$.ajax({
			"url": window.eu.login,
			"type": "POST",
			"data": user,
			"dataType": "json",
			"success": function(response) {
				if (response.success == 1) {
					//关闭OAuth授权窗口（如果有）
					if (OAuthWindow !== null && typeof(OAuthWindow.close) === 'function') {
						OAuthWindow.close();
					}
					window.parent[window.eu.callback]();
				} else {
					alert('用户名或密码错误')
					$('#submit').removeAttr('disabled');
					$('#submit').html('登录');
				}
			},
			"error": function(response) {
				alert('网络错误');
				$('#submit').removeAttr('disabled');
				$('#submit').html('登录');
			}
		});
	});
	//OAuth登录
	$('.oauth').bind('click', function() {
		var url = $(this).attr('data-url');
		OAuthWindow = window.open(url, 'OAuthWindow', 'menubar=no,status=no,toolbar=no');
		//注入回调函数
		if (typeof(window.OAuthCallback) === 'undefined') {
			window.OAuthCallback = function() {
				if (OAuthWindow !== null && typeof(OAuthWindow.close) === 'function') {
					OAuthWindow.close();
				}
				window.parent[window.eu.callback]();
			};
		}
	});
})(jQuery);